<template>
  <el-dialog title="产品BOM物料选择" v-if="showFlag" :visible.sync="showFlag" :modal="false" width="60%" center
    :close-on-click-modal="false">
    <!-- <el-form
        :model="queryParams"
        ref="queryParams"
        size="small"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-form-item label="物料编码" prop="itemCode">
          <el-input
            v-model="queryParams.itemCode"
            placeholder="请输入物料编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
  
        <el-form-item label="物料名称" prop="sonname">
          <el-input
            v-model="queryParams.sonname"
            placeholder="请输入物料名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
    </el-form>-->
    <div class="per">
      <el-table v-loading="loading" :data="bomList" @current-change="handleCurrent" @row-dblclick="handleRowDbClick"
        @selection-change="handleSelectionChange" max-height="620" row-key="bomItemId"
        :row-class-name="tableRowClassName">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="序号" align="center" width="50px">
          <template slot-scope="scope">
            <span>{{ scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <!--      <el-table-column width="50" align="center">-->
        <!--        <template v-slot="scope">-->
        <!--          <el-radio v-model="selectedItemId" :label="scope.row.bomItemId" @change="handleRowChange(scope.row)">-->
        <!--            {{ "" }}-->
        <!--          </el-radio>-->
        <!--        </template>-->
        <!--      </el-table-column>-->
        <el-table-column label="物料编码" align="center" prop="itemCode" />
        <el-table-column label="物料名称" align="center" prop="sonname" />
        <el-table-column label="规格" align="center" prop="specification" />

        <el-table-column label="用量:分子" align="center" show-overflow-tooltip prop="jdyNumerator"></el-table-column>

        <el-table-column label="用量:分母" width="90px" align="center" prop="jdyDenominator">
          <template slot-scope="scope">
            <span>{{ scope.row.jdyDenominator ? scope.row.jdyDenominator : 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="单位" align="center" prop="measureName" />
        <!-- <el-table-column label="使用比例" align="center" prop="quantity"/> -->
        <!-- <el-table-column
          label="备注"
          align="center"
          prop="remark"
          :show-overflow-tooltip="true"
        />-->
      </el-table>
    </div>
    <!-- <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />-->
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirmSelect">确 定</el-button>
      <el-button @click="showFlag = false">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { listBom, sonList } from "@/api/mes/md/bom";
import { addBom, updateRouteproductbom } from "@/api/mes/pro/routeproductbom";

export default {
  name: "ItemBomSelectSingle",
  props: {
    itemId: undefined,
    routeId: undefined,
    recordId: undefined,
    versionsId: undefined,
    processCode: undefined,
    processName: undefined,
    processId: undefined
  },
  data() {
    return {
      showFlag: false,
      // 遮罩层
      loading: true,
      selectedItemId: undefined,
      selectedRows: undefined,
      selectedData: undefined,
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品BOM关系表格数据
      bomList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        // productId: this.itemId,
        pageNum: 1,
        pageSize: 999,
        // itemId: this.itemId,
        bomId: null,
        jdyItemId: this.jdyItemId,
        bomItemId: null,
        bomItemCode: null,
        itemId: null,
        specification: null,
        bomItemSpec: null,
        unitOfMeasure: null,
        quantity: null,
        enableFlag: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null
      },
      //新增确定后保存的id数组
      Ids: [],
      //回显的list
      list: [],
      form: {},
      //重复添加需要抛出报错的id
      repeatList: []
    };
  },
  created() {
    // this.getList();
  },
  methods: {
    init(row, repetitive, data, obj) {
      console.log(row, data, obj, "回显的row");
      this.list = row || [];
      this.repetitive = repetitive;
      this.form = { ...data, ...obj };
      this.queryParams.bomId = this.form.bomId;
      this.getList();
      console.log(this.versionsId, "versionsId");
    },
    /** 查询产品BOM关系列表 */
    getList() {
      //debugger;
      this.loading = true;
      let params = {
        bomItemCode: this.form.bomItemCode,
        itemId: this.form.itemId
      };
      sonList(params).then(response => {
        console.info("response.rows====bom2222====>", response);
        this.bomList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    //搜索
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryParams");
      this.handleQuery();
    },
    handleCurrent(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    handleRowDbClick(row) {
      if (row) {
        this.selectedRows = row;
        this.$emit("onSelected", this.selectedRows);
        this.showFlag = false;
      }
    },
    // 单选选中数据
    handleRowChange(row) {
      if (row) {
        this.selectedRows = row;
      }
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      console.info("selection===", selection);
      this.selectedData = selection;
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
      this.ids = selection.map(item => item.itemCode);
      let list = this.repetitive.map(item => item.itemCode);
      let repetitive = this.compare(list, this.ids);
      this.repeatList = repetitive;
      console.log(repetitive, "之前提交了 现在又选了的");
      if (repetitive.length != 0) {
        this.$modal.msgError("产品物料编码为" + repetitive + "已被选择");
      }
    },

    //确定选中
    confirmSelect() {
      if (this.ids == null || this.ids == 0) {
        this.$notify({
          title: "提示",
          type: "warning",
          message: "请至少选择一条数据!"
        });
        return;
      } else {
        if (this.repeatList.length != 0) {
          this.$modal.msgError("产品物料编码为" + this.repeatList + "已被选择");
          return;
        }
        this.showFlag = false;
        console.log(this.form, "=======this.form=======");
        this.selectedData.forEach(obj => {
          obj.processId = this.form.processId;
          obj.processCode = this.form.processCode;
          obj.processName = this.form.processName;
          obj.unitOfMeasure = obj.measureName;
          obj.routeId = this.routeId;
          obj.recordId = this.recordId;
          obj.versionsId = this.versionsId;
          obj.productId = this.form.itemId;
          obj.bomItemCode = this.form.bomItemCode;
          obj.workorderId = this.form.workorderId;
          obj.productId = obj.itemId;
          obj.itemId = obj.bomItemId;
          obj.itemName = obj.sonname;
        });
        console.log(this.selectedData, "=====selectdata=============");
        addBom(this.selectedData).then(response => {
          console.info("response.rows====bom====>", response);
          this.open = false;
          this.ids = null;
          this.selectedData = [];
          this.$emit("backEndData");
        });
      }
    },
    compare(arr1, arr2) {
      return arr1.filter(v => {
        return arr2.includes(v);
      });
    },
    //改变颜色
    tableRowClassName({ row, rowIndex }) {
      // console.log(row, "row");
      // console.log(this.list, "this.list");
      let bool = this.list.filter(_ => _.itemCode == row.itemCode);
      if (bool && bool.length > 0) {
        return "success-row";
      }
      return "";
    }
  }
};
</script>
<style scoped>
.per ::v-deep .el-table .warning-row {
  background: oldlace;
}

.per ::v-deep .el-table .success-row {
  background: rgba(107, 178, 241, 0.722);
}

.per ::v-deep .el-table tbody tr:hover>td {
  background-color: unset !important;
}
</style>